package ServletHandle;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



public class LoginHandle extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session=request.getSession();
		String userId=request.getParameter("UserName");//获取前台url传过来的uName参数
		String userPwd=request.getParameter("UserPwd");//获取前台url传过来的uPwd参数
		DAL.Login _login=new DAL.Login();//实例化Login对象，来至DAL包
		boolean result=_login.checkLogin(userId, userPwd);//检查登陆用户是否合法
		if(result)//登陆正确
		{
			session.setAttribute("UserId", userId);//将用户userId保存在session对象中全局使用
			String uName=_login.getName(userId);//获取用户名
			session.setAttribute("UserName", uName);
			String sysLevel=_login.getSysLevel(userId);
			session.setAttribute("UserLevel", sysLevel);
			request.getRequestDispatcher("/Index.jsp").forward(request, response);
			
			
			
		}
		else {//登陆错误
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out=response.getWriter();
			out.write("<script>alert('用户名或密码错误！');location.href='Login.jsp';</script>");
		}
	}
	
			
}
